package com.xia.dao.impl;

import com.xia.dao.BaseDao;
import com.xia.dao.StudentDao;
import com.xia.entity.Student;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl extends BaseDao implements StudentDao {
    @Override
    public List<Student> selectAll() {
        List<Student> students = new ArrayList<>();
        String sql = " SELECT * FROM studentinfo ";
        ResultSet rs = executeQuery(sql);
        if (rs != null){
            try {
                while (rs.next()){
                    Student student = new Student();
                    student.setSid(rs.getInt("sid"));
                    student.setSname(rs.getString("sname"));
                    student.setSgender(rs.getString("sgender"));
                    student.setSage(rs.getInt("sage"));
                    student.setSaddress(rs.getString("saddress"));
                    student.setSemail(rs.getString("semail"));
                    students.add(student);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closedAll(rs);
            }
        }
        return students;
    }


    @Override
    public int updateStudent(Student student) {
        StringBuilder sb = new StringBuilder();

        sb.append(" UPDATE studentinfo SET  ");
        sb.append(" sname = ?,sgender = ?,sage = ?,saddress = ?,semail = ? ");
        sb.append(" WHERE sid = ?");

        return executeUpdate(sb.toString(), student.getSid(),
                student.getSname(),student.getSgender(),
                student.getSage(),student.getSaddress(),student.getSemail());
    }

    @Override
    public int deleteStudent(Integer sid) {
        String sql = "DELETE FROM studentinfo WHERE sid = ?";

        return executeUpdate(sql,sid);
    }
}
